package org.shagin.jfin.sheet;

import java.util.ArrayList;
import java.util.List;

import org.shagin.jfin.data.model.OperationCategory;
import org.shagin.jfin.data.model.OperationCategoryLeaf;
import org.shagin.jfin.data.model.OperationRule;

/**
 * The Class SheetColumnCategory.
 */
public class SheetColumnCategory extends SheetColumnDouble {

	private OperationCategory category;

	/**
	 * Instantiates a new sheet column category.
	 *
	 * @param category the category
	 */
	public SheetColumnCategory(OperationCategory category) {
		super(category.getPath("/"));
		this.category = category;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getTitle() {
		// TODO Localize
		return "ИТОГО";
	}

	/**
	 * Gets the nested cols.
	 *
	 * @param sheet the sheet
	 * @return the nested cols
	 */
	public List<SheetColumnOperationRule> getNestedCols(Sheet sheet) {
		List<SheetColumnOperationRule> cols = new ArrayList<SheetColumnOperationRule>();
		populate(sheet, category, cols);
		return cols;
	}

	/**
	 * Populate.
	 *
	 * @param sheet the sheet
	 * @param cat the cat
	 * @param cols the cols
	 */
	private void populate(Sheet sheet, OperationCategory cat,
			List<SheetColumnOperationRule> cols) {
		if (cat instanceof OperationCategoryLeaf) {
			OperationCategoryLeaf catLeaf = (OperationCategoryLeaf) cat;
			for (OperationRule rule : catLeaf.getRules()) {
				SheetColumnOperationRule col = sheet.fundColumnByRule(rule);
				if (null != col)
					if (!cols.contains(col))
						cols.add(col);
			}
		} else
			for (OperationCategory c : cat.getChilds())
				populate(sheet, c, cols);
	}

	/**
	 * Gets the category.
	 *
	 * @return the category
	 */
	public OperationCategory getCategory() {
		return this.category;
	}
}
